<?php  
class News_UI_Bookmarks
{
    var $Controller;
    function News_UI_Bookmarks($controller)
    {  
    
        $this->Controller = $controller;   
        $this->Bookmarks = $this->GetBookmarks($controller->Settings['param']); 
    }
     
    
    function GetBookmarks($param = NULL)
    { 
        $dir       = 'data/mark/';
        $groupdirs = $this->Controller->OpenDir($dir); 
        $ret       = array();
        for ($x=0;$x<sizeof($groupdirs);$x++)
        { 
            $file         = $groupdirs[$x]['name'];
            $path         = $groupdirs[$x]['path']; 
            if (file_exists($path))
            { 
                $xml            = simplexml_load_file($path);
		        $dbfile         = $xml->xpath("//article"); 
		        $group          = $xml->xpath("//articles"); 
		        $name           = $group[0]->group;
		        $subj           = $dbfile[0]->subject; 
                if ($param == NULL || strlen($param) == 0 || strpos(strtolower($subj), strtolower($param))!==false)
				{
				    $ret[] = array('name'=>$name,'db'=>$dbfile, 'file'=>$file);	
				} 
            }
        }
		
		
        for($x = 0; $x < sizeof($ret); $x++) {
	  for($y = 0; $y < sizeof($ret); $y++) {  
		
	    if(strcmp($ret[$x]['db'][0]->subject, $ret[$y]['db'][0]->subject) < 0) {
	      $hold = $ret[$x];
	      $ret[$x] = $ret[$y];
	      $ret[$y] = $hold;
	    }
          }
		}
        return $ret;
    }
     

	function ViewasHiLo ($id, $data)
	{ 

            $gallery   = $this->Bookmarks[$id]['db']; 
	    $groupname = $this->Bookmarks[$id]['name'];
	    $markfile  = explode('.', $this->Bookmarks[$id]['file']);
	    array_pop($markfile);
	    $markindex = array_pop($markfile);

            $upindex   = $this->Controller->Settings['up'];
            $dnindex   = $this->Controller->Settings['dn']; 
            $paneindex = $this->Controller->Settings['pane'];
            $index     = $this->Controller->Settings['index']; 


            if (strlen($upindex) == 0) $upindex = sizeof($gallery);
	    if (strlen($dnindex) == 0) $dnindex = 0;
		 
            $matrix = $this->Controller->CreateHiLo ($dnindex, $upindex); 
	
 
		
		
            for ($i=0;$i<sizeof($matrix);$i++)
            {
                $x       = $matrix[$i]; 
                $item    = $gallery[$x];
	        $name    = $item->id; 
	        $value   = str_replace('&amp;','&',$item->subject);   
                $b64     = base64_encode ($name);
                $picname = "data/temp/{$groupname}/{$b64}";
                $check   = file_exists($picname) ? '' : 'checked';
	        $box     = "<input type='checkbox' {$check} value='{$name}' id='x_{$name}' name='rec[]'>";
	        $uri     = $this->Controller->URI (array('page'  => NULL,
	                                                 'edit'  => NULL,
	                                                 'group' => $groupname,
	                                                 'id'    => $id,
	                                                 'item'  => $x));
                $src     = "/ui/image/group/{$groupname}/id/{$name}/thumb/ok";
                $data['main'] .= $this->Controller->Factory->Thumbnail ($src, $uri, $box . $value . $x); 
            }
            return $this->Controller->Structure ($data); 
        }



    function Display ()
    { 
//        $factory = new News_UI_ControlFactory($this->Controller); 
		$data            = array();
//	    $blank           = "http://www.101stratford.com/webservices/images/clear.gif";
	    $data['toolbar'] = $this->Controller->Factory->TabBar('Bookmarks') . $this->Controller->Factory->BookmarkSearchForm(); 
	    $data['main']    = '';  
	    $data['header']  = "<a href='/ui/'>Home</a> &#187; ";
		$page  = $this->Controller->Settings['page'];
		$param = $this->Controller->Settings['param'];
	    if (strlen($page) == 0) $page = 1;
	    $picindex  = $this->Controller->Settings['item'];
	    if (strlen($picindex) == 0) $picindex = 0;
	    
    
		if (isset($this->Controller->Settings['group']))
		{     
		    $id        = $this->Controller->Settings['id'];
		    $gallery   = $this->Bookmarks[$id]['db']; 
		    $groupname = $this->Bookmarks[$id]['name'];
		    
		    $name      = $gallery[$picindex]->id; 
		    $value     = str_replace('&amp;','&', $gallery[$picindex]->subject);   
            $picname   = "/ui/image/group/{$groupname}/id/{$name}";
            $back      = $this->Controller->URI (array('group'   => NULL,
                                                       'id'      => NULL,
                                                       'item'    => NULL));
           
		    $find  = $this->Controller->URI (array('ui'      => 'articlefinder',
                                                   'pic'     => $name));
		    $bfind = $this->Controller->Factory->Button ($find, 'Find in ' . $groupname, 'i.p.cont.individual.gif');          
		            
		            
		    $data['header'] .=  "<a href='{$back}'>Bookmarks</a> &#187; ";
		    $data['header'] .=  "<a href='/ui/commandhandler/request/group/group/{$groupname}'>{$groupname}</a> &#187; {$value}";
//		    $data['header'] .= "<div style='float:right'>$bfind </div>";   `
		            
		    $data['main'] .= "<center>";
            $data['main'] .= "<a href='javascript:void(0)' onclick='advance(1)'><img id='box' border='0' height='540' src='{$picname}' /></a><br/>";
            $data['main'] .= "</center>"; 
		    
            $data['controlbar'] = "<select id='drp' onchange='view(this.options[this.selectedIndex].value)'>";  
            $z = 0;
            foreach($gallery as $item)
            {
		        $name    = $item->id; 
		        $value   = str_replace('&amp;','&',$item->subject);   
                $src     = "/ui/image/group/{$groupname}/id/{$name}";
                $data['controlbar'] .= sprintf("<option value='%s' %s>%s</option>", $src, $picindex==$z?'selected':'', $value); 
                $z ++;
            }
            $data['controlbar'] .= "</select>";
            $data['controlbar'] .= "<input type='button' value='Prev' onclick='advance(-1)' />";
            $data['controlbar'] .= "<input type='button' value='Next' onclick='advance(1)' />";
            $data['controlbar'] .= "<input type='button' id='btn' value='Play' onclick='Play(true)' /><br/>";
		    echo $this->Controller->Structure ($data);
		    return;
		}
	    
		if (isset($this->Controller->Settings['edit']))
		{     
                    if ($this->Controller->Settings['view'] == 'hilo')
		    {
			echo $this->ViewasHiLo($id, $data);
			return;
		    }

		    $id        = $this->Controller->Settings['edit'];
		    $gallery   = $this->Bookmarks[$id]['db']; 
		    $groupname = $this->Bookmarks[$id]['name'];
		    $markfile  = explode('.', $this->Bookmarks[$id]['file']);
		    array_pop($markfile);
		    $markindex = array_pop($markfile);
		    
                    $name      = $gallery[0]->id; 
                    $value     = str_replace('&amp;','&',$gallery[0]->subject);   
		    
                    $picname   = "/ui/image/group/{$groupname}/id/{$name}";
                    $back      = $this->Controller->URI (array('group'   => NULL,
                                                       'edit'    => NULL,
                                                       'page'    => NULL));
                    
                    $hilo      = array( 'ui'      => 'commandhandler', 
	                                'request' => 'markhilo', 
	                                'dn'      => '0', 
	                                'up'      => sizeof ($gallery),  
	                                'group'   => $groupname,  
	                                'index'   => $markindex
	                                );  
		    $bhilo     = $this->Controller->Factory->Button ($this->Controller->URI($hilo), 'Hi-Low', 'i.p.search.gif'); 
       
                    $data['header'] .= "<a href='{$back}'>Bookmarks</a> &#187; ";
                    $data['header'] .= "<a href='/ui/commandhandler/request/group/group/{$groupname}'>{$groupname}</a> &#187; {$value}";
                    $data['header'] .= "<div style='float:right'>";  
                    $data['header'] .= "<a href='javascript:void(0)' onclick='$(this.className).submit()' class='sub'>Delete selected</a>{$bhilo}";
                    $data['header'] .= "</div>";
		            
	            $hi     = $page * UI_PAGE_SIZE;
	            $low    = $hi - UI_PAGE_SIZE;
	    	
                    $ask    = array('ui'      => 'commandhandler', 
                                    'request' => 'markopen', 
                                    'page'    => NULL, 
                                    'edit'    => $id, 
                                    'group'   => $groupname,  
                                    'index'   => $markindex
                                    );  
                                                
                    $data['controlbar'] = $this->Controller->Factory->Paginator(sizeof($gallery), $ask);
	 
                    $uri = $this->Controller->URI (array('ui'      => 'commandhandler',
                                                         'request' => 'markedit',
                                                         'group'   => $groupname,
                                                         'index'   => $markindex));
		    $data['main'] .= "<form id='sub'  action='{$uri}' method='post'>";
                    for($x=$low;$x<$hi&&$x<sizeof($gallery);$x++)
                    {
                        $item    = $gallery[$x];
		        $name    = $item->id; 
		        $value   = str_replace('&amp;','&',$item->subject);   
                        $b64     = base64_encode ($name);
                        $picname = "data/temp/{$groupname}/{$b64}";
                        $check   = file_exists($picname) ? '' : 'checked';
		        $box     = "<input type='checkbox' {$check} value='{$name}' id='x_{$name}' name='rec[]'>";
		        $uri     = $this->Controller->URI (array('page'  => NULL,
		                                                 'edit'  => NULL,
		                                                 'group' => $groupname,
		                                                 'id'    => $id,
		                                                 'item'  => $x));
                        $src     = "/ui/image/group/{$groupname}/id/{$name}/thumb/ok";
                        $data['main'] .= $this->Controller->Factory->Thumbnail ($src, $uri, $box . $value); 
                    } 
                    $data['main'] .= "</form>";
		    echo $this->Controller->Structure ($data);
		    return;
		}
	    
	    
        $data['header'] .=  "Bookmarks";
		$hi     = $page * UI_PAGE_SIZE;
		$low    = $hi - UI_PAGE_SIZE;
        $data['controlbar'] = $this->Controller->Factory->Paginator(sizeof($this->Bookmarks), array('ui'=>'bookmarks', 'page'=>NULL));
	    
	    
//        for($x=0;$x<sizeof($this->Bookmarks);$x++) 
        for($x=$low;$x<$hi&&$x<sizeof($this->Bookmarks);$x++)
		{
		    $result    = $this->Bookmarks[$x]['db'];
		    $groupname = $this->Bookmarks[$x]['name'];
			$size      = sizeof ($result);
		    $markfile  = explode('.', $this->Bookmarks[$x]['file']);
		    array_pop($markfile);
		    $markindex = array_pop($markfile);
		    $name      = $result[0]->id; 
		    $value     = str_replace('&amp;','&',$result[0]->subject);   
            $b64       = base64_encode ($name);
            $picname   = "data/temp/{$groupname}/{$b64}";
            $go        = "/ui/commandhandler/request/group/group/{$groupname}";
			$link      = '<div align="center"><a href="'.$go.'"><b>' . $groupname . '</b></a></div>';
            $uri       = $this->Controller->URI(array('group'   => $groupname, 
                                                      'id'      => $x,
													  'param'   => $param));  
            $ask       = $this->Controller->URI(array('ui'      => 'commandhandler', 
                                                      'request' => 'markopen', 
                                                      'page'    => 1, 
                                                      'edit'    => $x, 
                                                      'group'   => $groupname,  
                                                      'index'   => $markindex,
													  'param'   => $param
                                                ));  
            $off       = $this->Controller->URI(array('ui'      => 'commandhandler', 
                                                      'group'   => $groupname, 
                                                      'page'    => $page, 
                                                      'index'   => $markindex,
													  'param'   => $param, 
                                                      'request' => 'markoff'));  
            
            $edit      = $this->Controller->Factory->Button ($ask, 'Edit', '/images/i.p.edit.gif') . // ' &bull; ' .
                         $this->Controller->Factory->Button ($off, 'Delete', '/images/i.p.delete.gif', "Do you want to delete this bookmark and all its images?");
//            $edit      = "<a href='{$ask}'>Edit</a> | <a href='{$off}'>Delete</a>";
            $src       = file_exists($picname) ? "/{$picname}" : "/ui/image/group/{$groupname}/id/{$name}";
            
            $src = "/ui/image/group/{$groupname}/id/{$name}/thumb/ok";
            
            $data['main'] .= $this->Controller->Factory->Thumbnail ($src, $uri, "{$edit} {$size} items{$link}{$value} <br/> "); 
		}   
	    echo $this->Controller->Structure ($data);
	    return; 
    }


}
 





